package heima.lm.oschina.ui.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;
import heima.lm.oschina.ui.activity.UserBlogListActivity;
import heima.lm.oschina.ui.activity.UserFavoriteActivity;
import heima.lm.oschina.ui.activity.UserMessageCenterActivity;
import heima.lm.oschina.ui.activity.UserNoteActivity;
import heima.lm.oschina.ui.activity.UserTeamActivity;
import heima.lm.oschina.ui.view.DiscoverItemView;
import heima.lm.oschina.utils.QRCodeUtil;

/**
 * Created by herenxue on 2017/4/1.
 * 我的Fragment
 */

public class UserInfoFragment extends Fragment {
    @BindView(R.id.ib_2_dimensional_bar_code)
    ImageButton dimensionCode;
    @BindView(R.id.div_my_msg)
    DiscoverItemView mDvMessage;
    @BindView(R.id.div_my_blog)
    DiscoverItemView mDvBlog;
    @BindView(R.id.div_my_note)
    DiscoverItemView mDvNote;
    @BindView(R.id.div_my_team)
    DiscoverItemView mDvTeam;
    @BindView(R.id.rl_favorite)
    RelativeLayout mRelativeLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        //绑定该视图
        View viewInfo = inflater.inflate(R.layout.fragment_myinfo, null);
        ButterKnife.bind(this, viewInfo);
        return viewInfo;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //消息
        mDvMessage.setImageResource(R.drawable.icon_my_message);
        mDvMessage.setTextContent("消息");

        //博客
        mDvBlog.setImageResource(R.drawable.icon_my_blog);
        mDvBlog.setTextContent("博客");
        //便签
        mDvNote.setImageResource(R.drawable.icon_my_note);
        mDvNote.setTextContent("便签");
        //团队
        mDvTeam.setImageResource(R.drawable.icon_my_team);
        mDvTeam.setTextContent("团队");
        initEvent();
    }

    private void initEvent() {
        //先完成二维码生成
        dimensionCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = BaseApplication.mApp.getCacheDir().getAbsolutePath() + "two_dimension.jpg";
                System.out.print(path);
                QRCodeUtil.createQRImage("开源中国", 200, 200, null, path);
                Bitmap bitmap = BitmapFactory.decodeFile(path);
                dimensionCode.setImageBitmap(bitmap);
            }
        });
        //设置信息栏的4个条目的点击
        //展示消息条目,当点击条目时跳转到另外的fragment,这里写点击事件跳转
        mDvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserMessageCenterActivity.class);
                startActivity(intent);
            }
        });
        mDvBlog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserBlogListActivity.class);
                startActivity(intent);
            }
        });
        mDvNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserNoteActivity.class);
                startActivity(intent);
            }
        });
        mDvTeam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserTeamActivity.class);
                startActivity(intent);
            }
        });
        mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserFavoriteActivity.class);
                startActivity(intent);
            }
        });
    }
}
